<template>
  <div class="pet-manage">
    <el-table
      v-loading="$store.state.loading.loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255, 255, 255, 0.8)"
      :data="filterPetList"
      border
      style="width: 100%"
    >
      <el-table-column type="expand">
        <template slot-scope="scope">
          <pet-status :petMsg="scope.row"></pet-status>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="宠物名">
      </el-table-column>
      <el-table-column align="center" prop="picture" label="图片">
        <template slot-scope="scope">
          <el-avatar
            shape="square"
            :size="60"
            :src="scope.row.pictureUrl"
          ></el-avatar>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="age" label="年龄">
      </el-table-column>
      <el-table-column align="center" prop="type" label="种类">
      </el-table-column>

      <el-table-column>
        <template slot="header" slot-scope="scope">
          <div class="operate">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入关键字搜索"
            />
            <!-- <el-button
              type="text"
              size="small"
              style="margin: 0 20px"
              @click="handleDialogVisible()"
              >新增</el-button
            > -->
          </div>
        </template>

        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleDialogVisible(scope.row)"
            >编辑</el-button
          >
          <el-button
            @click="handleDeletePet(scope.row)"
            type="text"
            size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="paginationMsg.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="paginationMsg.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="+paginationMsg.total"
      style="margin: 10px 0"
    >
    </el-pagination>
    <pet-manage-eitor-dialog
      :getPetListMsg="getPetListMsg"
      :paginationMsg="paginationMsg"
      :petMsg="petMsg"
      :dialogVisible="petManageEitorDialogVisible"
      :handleDialogVisible="handleDialogVisible"
      :colseDialogVisible="colseDialogVisible"
      :type="type"
      :typeMsg="typeMsg"
    ></pet-manage-eitor-dialog>
  </div>
</template>

<script>
import PetManageEitorDialog from "@/views/home/components/PetManageEitorDialog";
import PetStatus from "@/views/home/components/PetStatus";
import { getPetList, deletePet } from "@/apis/home";
import { EDITOR, ADD, EDITOR_MESSAGE, ADD_MESSAGE } from "@/configs/constant";
export default {
  components: {
    PetManageEitorDialog,
    PetStatus,
  },
  mounted() {
    const { pageNum, pageSize } = this.paginationMsg;
    this.getPetListMsg(pageNum, pageSize);
  },

  methods: {
    handleDialogVisible(petMsg) {
      this.petManageEitorDialogVisible = !this.petManageEitorDialogVisible;
      if (petMsg) {
        this.petMsg = petMsg;
        this.type = EDITOR;
        this.typeMsg = EDITOR_MESSAGE;
      } else {
        this.type = ADD;
        this.typeMsg = ADD_MESSAGE;
      }
      if (!this.petManageEitorDialogVisible) {
        this.petMsg = { ...this.petMsgCopy };
      }
    },
    colseDialogVisible() {
      this.petManageEitorDialogVisible = false;
      this.petMsg = { ...this.petMsgCopy };
      this.$message.info(`已取消${this.typeMsg}`);
    },
    handleDeletePet(row) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const data = await deletePet(row.id);
          this.getPetListMsg();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(val) {
      const { pageNum } = this.paginationMsg;
      this.getPetListMsg(pageNum, val);
    },
    handleCurrentChange(val) {
      const { pageSize } = this.paginationMsg;
      this.getPetListMsg(val, pageSize);
    },
    async getPetListMsg(pageNum, pageSize) {
      const { data } = await getPetList(pageNum, pageSize);
      this.petList = data.list;
      this.paginationMsg = data;
      this.totalReserve = this.paginationMsg.total;
    },
  },
  computed: {
    filterPetList() {
      if (!this.search) {
        this.paginationMsg.total = this.totalReserve;
        return this.petList;
      }
      let myPetList = this.petList.filter(
        (pet) =>
          !this.search ||
          pet.name.toLowerCase().includes(this.search.toLowerCase())
      );
      this.paginationMsg.total = myPetList.length;
      return myPetList;
    },
  },

  data() {
    return {
      search: "",
      petList: [],
      paginationMsg: {
        pageNum: 1,
        pageSize: 5,
        total: 0,
        totalPage: 0,
      },
      totalReserve: 0,
      petManageEitorDialogVisible: false,
      petMsg: {
        id: 0,
        type: "",
        pictureUrl: "",
        userId: 0,
        age: 0,
        name: "",
      },
      petMsgCopy: {
        id: 0,
        type: "",
        pictureUrl: "",
        userId: 0,
        age: 0,
        name: "",
      },
      type: "",
      typeMsg: "",
    };
  },
};
</script>

<style lang="less" scoped>
.pet-manage {
  width: 100%;
  height: 100%;
  .operate {
    display: flex;
    align-items: center;
  }
}
</style>
